<?php
/**
 */
class GroupsTable extends Doctrine_Table
{
	
	public function getGroupDetails($user_id,$group_type,$currentPage,$resultPerPage)
	{
		$query=new Doctrine_Query();
		    $query->select('g.*')
		       ->from('Groups g')
			   ->where("owner='".$user_id."'");
			   //->andWhere("group_type='".$group_type."'");
			   //->andWhere("status=1");
			    //echo $query;exit;
			   	require_once('../database/models/Pager.php');
			$pager = new Pager($currentPage,$resultsPerPage);
			//echo $query->getSql();
			$result = $pager->pagination($query);
			//echo '<pre>';print_r($result);exit;
			return $result;
	}
	public function getGroupsById($user_id,$group_type,$status)
	{
		$query=new Doctrine_Query();
		    $query->select('g.*')
		       ->from('Groups g')
			   ->where("owner='".$user_id."'")
			   //->andWhere("group_type='".$group_type."'")
			   ->andWhere("status=1");
			   //->andWhere("status=1");
			    //echo $query->getSqlQuery();
			   	$result = $query->execute(array(),Doctrine::HYDRATE_ARRAY);
				//echo '<pre>';print_r($result);exit;
				return $result;
	}
	public function getGroupDetailsUsingId($group_id)
	{
		$query=new Doctrine_Query();
		    $query->select('g.*')
		       ->from('Groups g')
			   ->where("g.group_id='".$group_id."'")
			   //->andWhere("group_type='".$group_type."'")
			   ->andWhere("status=1");
			   //->andWhere("status=1");
			    //echo $query->getSqlQuery();
			   	$result = $query->execute(array(),Doctrine::HYDRATE_ARRAY);
				//echo '<pre>';print_r($result);exit;
				return $result;
	}
	public function getGroupsByUserId($user_id,$group_type,$status)
	{
		$query=new Doctrine_Query();
		    $query->select('g.*')
		       ->from('Groups g')
			   ->where("owner='".$user_id."'")
			   ->andWhere("group_type='".$group_type."'")
			   ->andWhere("status=1");
			    //echo $query;exit;
			$result['result'] = $query->execute(array(),Doctrine::HYDRATE_ARRAY);
				//echo '<pre>';print_r($result);exit;
				return $result;
	}
	
	public function getGroupDetailsByid($user_id,$group_id)
	{
		$query=new Doctrine_Query();
		    $query->select('g.*')
		       ->from('Groups g')
			   ->where("owner='".$user_id."'")
			   ->where("group_id='".$group_id."'");
			   //->andWhere("status=1");
			    //echo $query;exit;
			   	$result = $query->execute(array(),Doctrine::HYDRATE_ARRAY);
				//echo '<pre>';print_r($result);exit;
				return $result[0];
	}
	public function updateGroup($memid,$name,$desc,$fsname,$group_id)
	{
		$query = new Doctrine_Query();
		$query->update('Groups')
			  ->set('name',"'$name'")
			  ->set('description',"'$desc'")
			  ->set('logo',"'$fsname'")
			  ->where("group_id=".$group_id)
			  ->andWhere("owner=".$memid);
			  //echo $query;exit;
		return $query->execute();
	}
	public function updateGroups($memid,$name,$desc,$group_id)
	{
		$query = new Doctrine_Query();
		$query->update('Groups')
			  ->set('name',"'$name'")
			  ->set('description',"'$desc'")
			  ->where("group_id=".$group_id)
			  ->andWhere("owner=".$memid);
			  //echo $query;exit;
		return $query->execute();
	}
	
	public function getGroupName($user_id,$group_name,$group_type)
	{
		$query=new Doctrine_Query();
		    $query->select('count(group_id) as numCount')
		       ->from('Groups g')
			   ->where("g.name='".$group_name."'")
			   ->andWhere("g.owner='".$user_id."'")
			   ->andWhere("g.group_type='".$group_type."'");
			    //echo $query;exit;
			   	$result = $query->execute(array(),Doctrine::HYDRATE_ARRAY);
				//echo '<pre>';print_r($result);exit;
				return $result[0];
	}
	public function changeStatusGroup($memid,$status)
	{
		$query = new Doctrine_Query();
		$query->update('Groups')
			  ->set('status',$status)
			  ->where("owner=".$memid);
			  //echo $query;exit;
		return $query->execute();	  
	}
	public function changeStatusGroupId($memid,$status,$group_id)
	{
		$query = new Doctrine_Query();
		$query->update('Groups')
			  ->set('status',$status)
			  ->where("owner=".$memid)
			  ->andWhere("group_id IN (".$group_id.")");
			  //echo $query;exit;
		return $query->execute();
	}
	public function addGroup($user_id,$group_name,$desc,$fsname,$group_type)
	{
		$Groups = new Groups();
		$Groups ->owner = $user_id;
		$Groups ->name = $group_name;
		$Groups ->group_type = $group_type;
		$Groups ->description = $desc;
		$Groups ->logo = $fsname;
		$Groups ->created_time = date('Y-m-d H:i:s');
		$Groups ->status = 1;
		//echo '<pre>';print_r($Groups);exit;
		$Groups ->save();
		return $Groups->group_id;
	}
	public function getGroupDetailsByGroupId($group_id,$group_type)
	{
		$query=new Doctrine_Query();
		    $query->select('g.name')
		       ->from('Groups g')
			   ->where("g.group_id='".$group_id."'")
			   ->andWhere("g.group_type='".$group_type."'");
			    //echo $query;exit;
			   	$result = $query->execute(array(),Doctrine::HYDRATE_ARRAY);
				//echo '<pre>';print_r($result);exit;
				return $result[0];
	}
}